﻿using Kingdee.BOS;
using Kingdee.BOS.App;
using Kingdee.BOS.Contracts;
using Kingdee.BOS.Core.Report;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.K3.FIN.CB.App.Report;
using System.ComponentModel;

namespace Caraya.MFG.BusinessPlugIn
{
    [Description("二开材料耗用成本查询"), HotUpdate]
    public class CB_MATERIALCOSTRPTEX : MaterialCostRpt
    {
        private string[] customTableNames;
        public override ReportHeader GetReportHeaders(IRptParams filter)
        {
            ReportHeader reportHeader = base.GetReportHeaders(filter);
            reportHeader.AddChild("FSaleOrderNo", new LocaleValue("需求单据", 2052));
            reportHeader.AddChild("FProductName", new LocaleValue("销售订单产品名称", 2052));
            reportHeader.AddChild("FDataValue", new LocaleValue("产品类别", 2052));
            return reportHeader;
        }
        public override void BuilderReportSqlAndTempTable(IRptParams filter, string tableName)
        {
            IDBService dbService = ServiceHelper.GetService<IDBService>();
            customTableNames = dbService.CreateTemporaryTableName(this.Context, 1);
            string strTable = customTableNames[0];
            base.BuilderReportSqlAndTempTable(filter, strTable);

            string text = @"/*dialect*/
SELECT t1.* ,e0.FSaleOrderNo,e0.FProductName,e0.FDataValue
into {0}
FROM {1} t1
INNER JOIN
    (SELECT   v0.FSaleOrderNo, v2.FName AS FProductName, v0.FSEQ, v1.FBillNo, v3.FDataValue
FROM      (SELECT   t1.FSEQ, t0.FID, t0.FPRDORGID AS FPrdOrgId, t1.F_PRODUCTNAME AS F_ProductName, 
                                 t1.FSALEORDERNO AS FSaleOrderNo
                 FROM      T_PRD_MO AS t0 LEFT OUTER JOIN
                                 T_PRD_MOENTRY AS t1 ON t0.FID = t1.FID
                 WHERE   (t0.FFORMID = 'PRD_MO')) AS v0 LEFT OUTER JOIN
                    (SELECT   FID, FBILLNO AS FBillNo
                     FROM      T_PRD_MO AS t0
                     WHERE   (FFORMID = 'PRD_MO')) AS v1 ON v0.FID = v1.FID LEFT OUTER JOIN
                    (SELECT   t0.FMATERIALID, t0.FNUMBER, t0.F_BP_PROTYPES AS F_BP_ProTypes, t0_L.FNAME AS FName
                     FROM      T_BD_MATERIAL AS t0 LEFT OUTER JOIN
                                     T_BD_MATERIAL_L AS t0_L ON t0.FMATERIALID = t0_L.FMATERIALID AND t0_L.FLOCALEID = 2052) 
                AS v2 ON v0.F_ProductName = v2.FMATERIALID LEFT OUTER JOIN
                    (SELECT   t0.FENTRYID AS FEntryID, t0_L.FDATAVALUE AS FDataValue
                     FROM      T_BAS_ASSISTANTDATAENTRY AS t0 LEFT OUTER JOIN
                                     T_BAS_ASSISTANTDATAENTRY_L AS t0_L ON t0.FENTRYID = t0_L.FENTRYID AND t0_L.FLOCALEID = 2052) 
                AS v3 ON v2.F_BP_ProTypes = v3.FEntryID
) AS e0
ON t1.FORDERNUMBER = e0.FBillNo AND t1.FORDERNUMBERSEQ = e0.FSEQ
";

            var sql = string.Format(text, tableName, strTable);
            DBServiceHelper.ExecuteDynamicObject(this.Context, sql);
        }
        public override void CloseReport()
        {
            if (customTableNames.IsNullOrEmptyOrWhiteSpace())
            {
                return;
            }
            IDBService dbService = ServiceHelper.GetService<IDBService>();
            dbService.DeleteTemporaryTableName(this.Context, customTableNames);
            base.CloseReport();
        }
    }
}
